#include "OpeningScene.hpp"

OpeningScene::OpeningScene() {
    totalTime = 0.0f;
    alpha = 0.0f;
    isLogoReady = false;
}

void OpeningScene::Load() {
    totalTime = 0.0f;
    alpha = 0.0f;

    #ifdef _DEBUG
    std::cout << "OpeningScene::Load" << std::endl;
    #endif

    // Load image if it's loaded
    if (!isLogoReady) {
        logoImage.LoadFromFile("resource\\logo2.png");

        logoSprite.SetImage(logoImage);
        logoSprite.SetX(0);
        logoSprite.SetY(0);

        isLogoReady = true;
    }
}

void OpeningScene::Unload() {
    #ifdef _DEBUG
    std::cout << "OpeningScene::Unload" << std::endl;
    #endif
}

void OpeningScene::Update(sf::Event& event, sf::RenderWindow& app) {
    // Switch to main menu scene in five second
    totalTime += app.GetFrameTime();

    if (alpha >= 255) {
        logoSprite.SetColor(sf::Color(255, 255, 255, 255));
    } else {
        alpha += app.GetFrameTime() * 110;
        logoSprite.SetColor(sf::Color(255, 255, 255, alpha));
    }

    // Set it to middle
    logoSprite.SetX((app.GetWidth() - logoImage.GetWidth()) / 2);
    logoSprite.SetY((app.GetHeight() - logoImage.GetHeight()) / 2);

    if (totalTime > 5.0f) {
        manager->Switch("Game");
    }
}

void OpeningScene::Draw(sf::RenderWindow& app) {
    app.Clear(sf::Color(255, 255, 255));

    if (isLogoReady) {
        app.Draw(logoSprite);
    }
}
